package com.ngc.mall.controller;

import com.ngc.mall.common.configure.LocalUser;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.IntegralSpuDomain;
import com.ngc.mall.domain.UserIntegralLogDomain;
import com.ngc.mall.domain.UserIntegralTaskDomain;
import com.ngc.mall.service.IIntegralSpuService;
import com.ngc.mall.service.IUserIntegralLogService;
import com.ngc.mall.service.IUserIntegralTaskService;
import com.ngc.mall.vo.PagingVO;
import com.ngc.mall.vo.UserIntegralVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * 积分
 * @author: chenxiaolin
 * @date 2023/9/12 12:00
**/
@RestController
@RequestMapping("/integral")
public class IntegralController {
    @Autowired
    private IIntegralSpuService iIntegralSpuService;

    @Autowired
    private IUserIntegralTaskService iUserIntegralTaskService;

    @Autowired
    private IUserIntegralLogService iUserIntegralLogService;

    @GetMapping("/integral_spu/list/show")
    @ApiOperation("查看积分商品列表(已启用)")
    public ResponseResult<PagingVO<IntegralSpuDomain>> findIntegralSpuListIsShow(
            @RequestParam(name = "start", defaultValue = "0")
                    Integer start,
            @RequestParam(name = "count", defaultValue = "8")
                    Integer count
    ){
        return ResponseResult.success(iIntegralSpuService.selectAllShow(start,count));
    }

    @PostMapping("/integral_spu/exchange")
    @ApiOperation("兑换积分商品")
    public ResponseResult<Boolean> exchangeIntegralSpu(@RequestParam(name="id") Long integralSpuId){
        Long uid=LocalUser.getUser().getId();
        return ResponseResult.auto(iUserIntegralLogService.exchangeIntegralSpu(uid,integralSpuId));
    }

    @GetMapping("/task/list/show")
    @ApiOperation("获取所有积分任务(已启用）")
    public ResponseResult<List<UserIntegralTaskDomain>> getUserIntegralTask(){
        return ResponseResult.success(iUserIntegralTaskService.selectAllShow());
    }

    @PostMapping("/sign")
    @ApiOperation("签到")
    public ResponseResult<Boolean> sign(){
        Long uid= LocalUser.getUser().getId();
//        Long uid=Long.valueOf("1679736432946819074");
        LocalDateTime now=LocalDateTime.now();
        return ResponseResult.auto(iUserIntegralLogService.sign(uid,now));
    }

    @GetMapping("/my/detail")
    @ApiOperation("获取个人积分详情(总积分，签到情况，连续签到情况）")
    public ResponseResult<UserIntegralVO> getUserIntegralDetail(){
//        Long uid=Long.valueOf("1679736432946819074");
        Long uid= LocalUser.getUser().getId();
        LocalDateTime now=LocalDateTime.now();
        return ResponseResult.success(iUserIntegralLogService.getUserIntegralDetail(uid,now));
    }

    @GetMapping("/my/task")
    @ApiOperation("查询个人积分任务完成情况")
    public ResponseResult<List<UserIntegralTaskDomain>> queryMyTask(){
        LocalDateTime now=LocalDateTime.now();
//        Long uid=Long.valueOf("1679736432946819074");
        Long uid= LocalUser.getUser().getId();
        return ResponseResult.success(iUserIntegralLogService.querySignInTask(uid,now));
    }

    @PostMapping("/task/take")
    @ApiOperation("完成积分任务")
    public ResponseResult<Boolean> signTask(@RequestParam(name ="tag") String tag){
        LocalDateTime now=LocalDateTime.now();
        Long uid= LocalUser.getUser().getId();
//        Long uid=Long.valueOf("1679736432946819074");
        return ResponseResult.auto(iUserIntegralLogService.saveSignInTask(uid,tag,now));
    }

    @GetMapping("/my/integral_log")
    @ApiOperation("获取个人积分流水明细")
    public ResponseResult<List<UserIntegralLogDomain>> findMyIntegralLog(){
//        Long uid=Long.valueOf("1679736432946819074");
        Long uid=LocalUser.getUser().getId();
        return ResponseResult.success(iUserIntegralLogService.findByUid(uid));
    }


}
